/**
 * 摄像机 - L5
 * Created by lonphy on 2015/5/22.
 */
(function (L5) {
	"use strict";
	L5.Camera = class {
		/**
		 * 摄像机构造器
		 * @param eye {L5.Vec3} 摄像机位置
		 * @param at {L5.Vec3} 观察的目标位置点
		 * @param up {L5.Vec3} 摄影坐标系Y轴上方向
		 */
		constructor(eye, at, up) {
			//let tan2 = eye.clone().direction(at).dot(up);
			this._pitch = 0;	// x轴旋转量
			this._yaw = 0;		// y轴旋转量
			this.eye = eye;		// 摄像机位置

			this._moveSpeed = 1;
			this._joggingAngle = 0;
		}
		// 摄像机移动速度
		set moveSpeed(speed) { this._moveSpeed = speed || 0; }
		get moveSpeed() { return this._moveSpeed; }

		/**
		 * 更新摄像机
		 */
		update(gl, fps, time) {
			// 帧频跳动补偿
			let map = L5.Input.getMapState(),
				speed = this._moveSpeed*fps/60,
				yawSpeed = speed*0.3,
				moved = 0;
			if (map.RISE) {
				this._pitch += 0.1;
			} else if (map.BOW) {
				this._pitch -= 0.1;
			}

			if(map.LEFT){
				this._yaw += yawSpeed;
			}else if(map.RIGHT) {
				this._yaw -= yawSpeed;
			}

			let roteDeg = this._yaw*Math.PI/180;

			if(map.UP){
				moved = 1;
				this.eye.x -= Math.sin(roteDeg) * speed;
				this.eye.z -= Math.cos(roteDeg) * speed;
			}else if(map.DOWN){
				this.eye.x += Math.sin(roteDeg) * speed;
				this.eye.z += Math.cos(roteDeg) * speed;
				moved = 1;
			}

			if (moved != 0) {
				this._joggingAngle += fps*0.6;
				this.eye.y += Math.sin(this._joggingAngle*Math.PI/180);
			}

			this.scenes.zoom(L5.Input.L5Mouse.lastZoom);
		}

		toArray() {
			let p = L5.Matrix.rotateX(-this._pitch*Math.PI/180);
			let y = L5.Matrix.rotateY(-this._yaw*Math.PI/180);
			let t = L5.Matrix.translate(-this.eye.x, -this.eye.y, -this.eye.z);
			return L5.Matrix.multiply(L5.Matrix.multiply(p,y), t);
		}
	};
})(window.L5);